package com.qtjf.qtmoneyservice.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.activity.VipRuleActivity;
import com.qtjf.qtmoneyservice.adapter.HomeRecyclerAdapter;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.Product;
import com.qtjf.qtmoneyservice.domain.ResultProduct;
import com.qtjf.qtmoneyservice.view.HomeRecyclerHeadView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.relex.circleindicator.CircleIndicator;


public class HomeFragment extends Fragment {

    LRecyclerView homeRecycler;
    List<Product> homeLists;
    HomeRecyclerAdapter homeAdapter;
    LRecyclerViewAdapter lRecyclerViewAdapter;
    View headView;
    ResultProduct resultProduct;
    Gson gson;
    ImageView ruleBt;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        initView(view);
        initData();
        initEvent();

        return view;
    }

    private void initView(View view) {

        homeRecycler= (LRecyclerView) view.findViewById(R.id.home_recycler);
        homeRecycler.setPullRefreshEnabled(false);
        headView=new HomeRecyclerHeadView(getContext()).getView();
        ruleBt= (ImageView) view.findViewById(R.id.rule_bt);
    }

    private void initData() {

        gson=new Gson();
        homeLists=new ArrayList<Product>();
        requestCall();
    }

    private void initEvent() {

        ruleBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }

    private void requestCall() {
        String url= Constant.HOMEADDRESS + "products/getProducts?usermobile="+Constant.getUsermobile();
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Log.d("999",response);
                if(response.length()>25) {
                    resultProduct = gson.fromJson(response, new TypeToken<ResultProduct>() {
                    }.getType());
                    homeLists=resultProduct.getData();
                    if(homeLists!=null&&homeLists.size()>0) {
                        if (homeAdapter == null) {
                            homeAdapter = new HomeRecyclerAdapter(homeLists,getContext());
                            lRecyclerViewAdapter = new LRecyclerViewAdapter(homeAdapter);
                            homeRecycler.setAdapter(lRecyclerViewAdapter);
                            homeRecycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                            lRecyclerViewAdapter.addHeaderView(headView);
                        } else {
                            lRecyclerViewAdapter.notifyDataSetChanged();
                            homeRecycler.refreshComplete();
                        }

                        // lRecyclerViewAdapter.addFooterView(View.inflate(getContext(),R.layout.home_footer,null));
                        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {

                            }
                        });
                    }
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(getContext(), "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }

}
